package cn.ztuo.bitrade.job;



import cn.ztuo.bitrade.constant.SysConstant;
import cn.ztuo.bitrade.constant.TransactionType;
import cn.ztuo.bitrade.entity.ContractCoin;
import cn.ztuo.bitrade.entity.ContractOrderPositionType;
import cn.ztuo.bitrade.entity.ContractPosition;
import cn.ztuo.bitrade.entity.MemberTransaction;
import cn.ztuo.bitrade.service.ContractCoinService;
import cn.ztuo.bitrade.service.ContractPositionService;
import cn.ztuo.bitrade.service.ContractWalletService;
import cn.ztuo.bitrade.service.MemberTransactionService;
import cn.ztuo.bitrade.util.BigDecimalUtils;
import cn.ztuo.bitrade.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
public class ContractClearing {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private ContractCoinService contractCoinService;

    @Autowired
    private ContractPositionService contractPositionService;

    @Autowired
    private ContractWalletService contractWalletService;

    @Autowired
    private MemberTransactionService transactionService;

    /**
     * 定点结算资金费用
     */
//    @Scheduled(cron = "0 0 4,12,20 * * ?")
    public void clearing(){
        List<ContractPosition> positions = contractPositionService.findAll();
        SimpleDateFormat simple = new SimpleDateFormat("HH");
        String hour = simple.format(new Date());
        positions.stream().forEach(position->{
            if(position.getContractTotalNum() <= 0) return;
            ContractCoin coin = contractCoinService.findBySymbol(position.getSymbol());
            //获取标记价格
            BigDecimal marked_price =  (BigDecimal) redisUtil.get(SysConstant.CONTRACT_MARKED_PRICE + coin.getSymbol());
            //获取合约数量
            BigDecimal account = new BigDecimal(position.getContractTotalNum());
            //获取最小下单量
            BigDecimal min_account = coin.getMinVolume();
            //计算仓位价值
            BigDecimal positionValue = BigDecimalUtils.mulRound(BigDecimalUtils.mulRound(account,min_account),marked_price);
            //获取资金费率
            BigDecimal capital_rate  =  (BigDecimal) redisUtil.get(SysConstant.CONTRACT_CAPITAL_RATE + coin.getSymbol());
            //计算资金费用
            BigDecimal cost = BigDecimalUtils.mulRound(positionValue,capital_rate);
            int is_success = contractWalletService.updateCost(position.getMemberId(),cost,position.getPositionType());
            if(is_success>0) {
                //增加资金费用记录
                //订单方向RISE=开多FALL=开空'
                if(cost.compareTo(BigDecimal.ZERO)>0) {
                    if(position.getPositionType() == ContractOrderPositionType.RISE) cost = cost.negate();
                }else{
                    if(position.getPositionType() == ContractOrderPositionType.RISE) cost = cost.abs();
                }
                MemberTransaction transaction = new MemberTransaction();
                transaction.setAmount(cost);
                transaction.setSymbol("USDT");
                transaction.setAddress("");
                transaction.setMemberId(position.getMemberId());
                transaction.setType(TransactionType.CAPITAL_COST);
                transaction.setFee(BigDecimal.ZERO);
                transaction.setCreateTime(new Date());
                transactionService.save(transaction);
                log.info(hour+"点结算资金费用成功");
            }else{
                log.error("ID:"+position.getMemberId()+"的用户，"+hour+"点结算资金费用失败");
            }
        });
    }
}
